<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\admin\Model\Newarr as arr;

class NewArr extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
       $data = Db::name('newarr as a,lamp_yshop as b')
       ->field('a.id,b.name,a.menu,a.pic,a.desc,a.status')
       ->where('a.sid = b.id')
       ->paginate(10);

        return view('admin@/Column/NewList',[
            'data' => $data
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $list = Db::name('yshop as a,lamp_yshop_menu as b')
        ->field('a.id as sid,a.name,b.id,b.menu,b.price')
        ->where('a.id = b.sid')
        ->paginate(10);
        return view('admin@Column/NewArr',[
            'list' => $list
        ]);
    }

    /**
     * [add 创建新品]
     * @param Request $request [description]
     */
    public function add(Request $request)
    {
        $data = $request->post();
        $id = $data['id'];
        $price = $data['price'];
        $list = Db::name('menu_pic as a,lamp_yshop_menu as b')->field('a.id,a.sid,a.pic,a.new,b.menu')->where('b.id = '.$id.' and b.sid = a.sid')->select();

        return view('admin@Column/addNew',[
            'list' => $list, 
            'price' => $price
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->post();
        // 模型调用

        // $list = new arr;
        // $list->data($data);
        // $list->save();
        $list = Db::name('newarr')->insert($data);
        
        if ($list) {
            return $this->success('成功',url('admin/NewArr/index'));
        }else{
            return $this->error('失败');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //查看是不是ajax传送
        if(!Request::instance()->isAjax()){
            $this->error('非法请求!');
        }

        $data = Db::name('newarr')->field('status')->where('id',$id)->find();
        $status['status'] = $data['status']==1?2:1;

        $result = db('newarr')->where('id',$id)->setField($status);

        if ($result && $status['status'] == 1) {
            $info = $status;
            $info['id'] = $id;
            $info['info'] = '该店铺已上新品板块!';
            return json($info);
        }else{
            $info = $status;
            $info['id'] = $id;
            $info['info'] = '该店铺已下新品板块!';
            return json($info);
        }
    }

        /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

        // 开启事务删除表关联
        Db::startTrans();
        try {
            $result = db('newarr')->delete($id);

            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
        }


        if (empty($e)) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = '新品删除成功!';
            return json($info);
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '数据出错,已回调!';
            return json($info);
            // 这里问下老师,为什么没有进入回这个区间?
        }
    }
}
